<template>
    <div class="flex bottomBox">
        <div class="boxShadow stationSetComponent">
            <el-form ref="ruleForm"  class="wifiForm">
                <p class="boldTitle">添加站点</p>
                <el-form-item class="">
                    <div class="flex formItem">
                        <div class="itemLeft">站点名称</div>
                        <div class="itemRight"><input type="text" placeholder="请输入站点名称" v-model="stationForm.stationName"></div>
                    </div>
                </el-form-item>

                <el-form-item>
                    <div class="flex formItem">
                        <div class="itemLeft">站点类型</div>
                        <div class="itemRight">
                            <el-select v-model="stationForm.type" placeholder="请选择站点类型" @change="changeStationType" size="mini" class="stationType">
                                <el-option
                                v-for="item in stationForm.stationType"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </el-form-item>

                <!-- 默认站点模式 -->
                <el-form-item class="" v-show="showStationMode">
                    <div class="flex formItem">
                        <div class="itemLeft">站点模式</div>
                        <div class="itemRight">
                            <el-select v-model="stationForm.mode" placeholder="请选择站点模式" size="mini" class="stationType">
                                <el-option
                                v-for="item in stationForm.stationMode"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </el-form-item>

                <!-- 站点列表模式 -->
                <el-form-item class="" v-show="showGotoStationMode">
                    <div class="flex formItem">
                        <div class="itemLeft">站点模式</div>
                        <div class="itemRight">
                            <el-select v-model="stationForm.gotoMode" placeholder="请选择站点模式" size="mini" class="stationType">
                                <el-option
                                v-for="item in this.$store.state.stationList"
                                :key="item.name"
                                :label="item.name"
                                :value="item.name">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </el-form-item>

                <!-- call列表模式 -->
                <el-form-item class="" v-show="showCallStationMode">
                    <div class="flex formItem">
                        <div class="itemLeft">站点模式</div>
                        <div class="itemRight">
                            <el-select v-model="stationForm.callMode" placeholder="请选择站点模式" size="mini" class="stationType">
                                <el-option
                                v-for="item in this.$store.state.trajectoryList"
                                :key="item.name"
                                :label="item.name"
                                :value="item.name">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </el-form-item>

                <!-- 声音站点模式 -->
                <el-form-item class="" v-show="showVoiceStationMode">
                    <div class="flex formItem">
                        <div class="itemLeft">站点模式</div>
                        <div class="itemRight">
                            <el-select v-model="stationForm.voiceMode" placeholder="请选择站点模式" size="mini" class="stationType">
                                <el-option
                                v-for="item in stationForm.voiceStationMode"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </el-form-item>

                <el-form-item class="" v-show="showFrameId">
                    <div class="flex formItem">
                        <div class="itemLeft">frame_id</div>
                        <div class="itemRight"><input type="text" placeholder="请输入frame_id" v-model="stationForm.frameId"></div>
                    </div>
                </el-form-item>

                <el-form-item class="" v-show="showSubscribeValue">
                    <div class="flex formItem">
                        <div class="itemLeft">发布-订阅器接收值</div>
                        <div class="itemRight"><input type="text" placeholder="请输入接收值" v-model="stationForm.subscribeValue"></div>
                    </div>
                </el-form-item>

                <el-form-item class="">
                    <div class="flex formItem">
                        <div class="itemLeft">范围(米)</div>
                        <div class="itemRight"><input type="text" placeholder="请输入站点范围" v-model="stationForm.scope"></div>
                    </div>
                </el-form-item>

                <el-form-item class="">
                    <div class="flex formItem">
                        <div class="itemLeft">超时时间(秒)</div>
                        <div class="itemRight"><input type="text" placeholder="请输入超时时间" v-model="stationForm.timeout"></div>
                    </div>
                </el-form-item>

                <el-form-item class="" v-show="showPosition">
                    <div class="localTitle">Positon</div>
                    <div class="flex formItem">
                        <div class="localInputItem">
                            <div class="itemLeft">x</div>
                            <div class="itemRight">
                                <input type="text" placeholder="" v-model="stationForm.positionX">
                            </div>
                        </div>

                        <div class="localInputItem">
                            <div class="itemLeft">y</div>
                            <div class="itemRight">
                                <input type="text" placeholder="" v-model="stationForm.positionY">
                            </div>
                        </div>

                        <div class="localInputItem">
                            <div class="itemLeft">z</div>
                            <div class="itemRight">
                                <input type="text" placeholder="" v-model="stationForm.positionZ">
                            </div>
                        </div>
                    </div>
                </el-form-item>

                <el-form-item class="" v-show="showOrientation">
                    <div class="localTitle">Orientation</div>
                    <div class="flex formItem">
                        <div class="localInputItem">
                            <div class="itemLeft">x</div>
                            <div class="itemRight">
                                <input type="text" placeholder="" v-model="stationForm.orientationX"  @change="changeOrientation">
                            </div>
                        </div>
                        <div class="localInputItem">
                            <div class="itemLeft">y</div>
                            <div class="itemRight">
                                <input type="text" placeholder="" v-model="stationForm.orientationY"  @change="changeOrientation">
                            </div>
                        </div>
                        <div class="localInputItem">
                            <div class="itemLeft">z</div>
                            <div class="itemRight">
                                <input type="text" placeholder="" v-model="stationForm.orientationZ"  @change="changeOrientation">
                            </div>
                        </div>
                        <div class="localInputItem">
                            <div class="itemLeft">w</div>
                            <div class="itemRight">
                                <input type="text" placeholder="" v-model="stationForm.orientationW"  @change="changeOrientation">
                            </div>
                        </div>
                    </div>
                </el-form-item>
    
                <el-form-item class="" v-show="showYaw">
                    <div class="flex formItem">
                        <div class="itemLeft">Yaw(deg)</div>
                        <div class="itemRight"><input type="text" placeholder="请输入Yaw" v-model="stationForm.yaw" @change="changeYaw"></div>
                    </div>
                </el-form-item>

                <div class="flex formBtns">
                    <el-button size="small" type="primary" @click="subUpdate('stationForm')">提交</el-button>
                </div>
            </el-form>
        </div>
    </div> 
</template>

<script>
import { isQuaternionValid, quaternionToYaw, thetaToQuaternion } from '../js/tf'
import { submitAddStation } from '../js/methods'
export default {
    name: 'stationAdd',
    components:{

    },
    data(){
        return{
            stationForm:{
                stationName:'',
                stationType: [{
                    value: 'goal',
                    label: '目标站点'
                }, {
                    value: 'dock',
                    label: '充电桩'
                }, {
                    value: 'timer',
                    label: '计时器'
                }, {
                    value: 'publisher',
                    label: '发布器'
                }, {
                    value: 'subscriber',
                    label: '订阅器'
                }, {
                    value: 'looper',
                    label: '循环'
                }, {
                    value: 'pubsuber',
                    label: '发布-订阅器'
                }, {
                    value: 'cmd_vel_set_sub',
                    label: '最大速度设置'
                }, {
                    value: 'cmd_vel',
                    label: '速度发布器'
                }, {
                    value: 'shell',
                    label: '命令发布器'
                }, {
                    value: 'sound_play',
                    label: '声音播放'
                }, {
                    value: 'initial_pose',
                    label: '初始位置'
                }, {
                    value: 'scan_marker',
                    label: '充电桩定位'
                }, {
                    value: 'shell_string',
                    label: 'Shell命令'
                }, {
                    value: 'pallet',
                    label: 'Pallet'
                }, {
                    value: 'bias',
                    label: 'Bias'
                }, {
                    value: 'call',
                    label: 'Call'
                }, {
                    value: 'goto',
                    label: 'Goto'
                }],
                type: '目标站点',
                stationMode:[{
                    value: 'LOOP',
                    label: '多次'
                }, {
                    value: 'LOOP2',
                    label: 'LOOP2'
                }, {
                    value: 'BACK',
                    label: 'BACK'
                }, {
                    value: 'BACK2',
                    label: 'BACK2'
                }, {
                    value: 'NONE',
                    label: '单次'
                }],
                mode:'多次',
                gotoMode: '',
                callMode: '',
                voiceMode:'播放',
                voiceStationMode:[{
                    value: 'LOOP',
                    label: '播放'
                }, {
                    value: 'LOOP2',
                    label: '停止'
                }, {
                    value: 'BACK',
                    label: '播放一次'
                },],
                frameId:'map',
                subscribeValue:'0',
                positionX:'',
                positionY:'',
                positionZ:'',
                orientationX:'',
                orientationY:'',
                orientationZ:'',
                orientationW:'',
                yaw:'',
                scope:'0.0',
                timeout:'0.0'
            },
            showFrameId: true,
            showSubscribeValue: false,
            showStationMode: true,
            showVoiceStationMode: false,
            showGotoStationMode: false,
            showCallStationMode: false,
            showPosition: true,
            showOrientation: true,
            showYaw:true
        }
    },
    methods:{
        //选择站点类型
        changeStationType(type){
            let that = this;
            that.showFrameId = false;
            that.showSubscribeValue = false;
            that.showStationMode = true,
            that.showVoiceStationMode = false;
            that.showGotoStationMode = false;
            that.showCallStationMode = false;
            that.showPosition = true;
            that.showOrientation = true;
            that.showYaw = true;
            switch(type){
                case 'goal':
                that.showFrameId = true;
                break;

                case 'pubsuber':
                that.showSubscribeValue = true;
                break;

                case 'sound_play':
                that.showStationMode = false;
                that.showVoiceStationMode = true;
                that.showGotoStationMode = false;
                that.showCallStationMode = false;
                break;

                case 'goto':
                that.showStationMode = false;
                that.showVoiceStationMode = false;
                that.showGotoStationMode = true;
                that.showCallStationMode = false;
                break;

                case 'call':
                that.showStationMode = false;
                that.showVoiceStationMode = false;
                that.showGotoStationMode = false;
                that.showCallStationMode = true;
                break;

                case 'initial_pose':
                that.showFrameId = true;
                break;

                default:
                
                break;
            }
        },

        //改变orientation
        changeOrientation(){
            let that = this;
            let orientation = {
                x: parseFloat(this.stationForm.orientationX),
                y: parseFloat(this.stationForm.orientationY),
                z: parseFloat(this.stationForm.orientationZ),
                w: parseFloat(this.stationForm.orientationW),
            }
            console.log(orientation)
            //有效的四元数转换
            if (isQuaternionValid(orientation)){
                let yawRad = quaternionToYaw(orientation);
                let yawDeg = yawRad * 180 / Math.PI;
                that.stationForm.yaw = yawDeg.toFixed(5);
            }else{
                that.$message({
                    showClose: true,
                    message: '四元数不合法',
                    type: 'warning'
                });
                return
            }
        },

        /**
         * 改变yaw,转换为四元数
         * **/
        changeYaw(){
            let that = this;
            let yawDeg = parseFloat(that.stationForm.yaw);
            let orientation = thetaToQuaternion(yawDeg/180*Math.PI);
            that.stationForm.orientationX = orientation.x.toFixed(5);
            that.stationForm.orientationY = orientation.y.toFixed(5);
            that.stationForm.orientationZ = orientation.z.toFixed(5);
            that.stationForm.orientationW = orientation.w.toFixed(5);
        },

        //提交
        subUpdate() {
            if(this.stationForm.type === '目标站点'){
                this.stationForm.type = 'goal'
            }
            if(this.stationForm.mode === '多次'){
                this.stationForm.mode = 'LOOP'
            }
            if(this.stationForm.voiceMode === '播放'){
                this.stationForm.voiceMode = 'LOOP'
            }
            submitAddStation(this.stationForm);
      }
    },

    //计算属性,小车姿态转换
    watch:{
       '$store.state.robotPosition':function(val){
            this.stationForm.positionX = val.positionX;
            this.stationForm.positionY = val.positionY;
            this.stationForm.positionZ = val.positionZ;
            this.stationForm.orientationX = val.orientationX;
            this.stationForm.orientationY = val.orientationY;
            this.stationForm.orientationZ = val.orientationZ;
            this.stationForm.orientationW = val.orientationW;
            this.stationForm.yaw = val.yaw;
        }
    }
}
</script>

<style scoped lang="less">
 .stationSetComponent{
     background-color: #ffffff;
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     padding: 10px;
     text-align: left;
     font-size: 14px;
     width: 100%;
 }
 .boldTitle{
     font-weight: bold;
     margin-bottom: 7.5px;
 }
 .formItem{
    padding: 10px 0;
     border-bottom: 1px solid #eeeeee;
    .itemLeft{
        flex: 4
    }
    .itemRight{
        flex: 6;
        input{
            width: 100%;
            height: 100%;
            display: block;            
        }
    }
 }
 .localTitle{
     text-align: left;
     padding: 10px 0 0 0;
 }
 .localInputItem{
     text-align: center;
     padding: 0 5px;
     input{
         border: 1px dashed #eeeeee;
         padding: 5px 0;
         text-align: center;
         margin-top: 5px;
     }
 }
 .stationType{
     input{
         height: 10px;
     }
     .el-input__icon{
         height:inherit
     }
     .el-input__inner{
         height: normal;
         line-height: normal
     }
    width: 100%;
        //清除select的边框样式
    border: none;
    //清除select聚焦时候的边框颜色
    outline: none;
    padding-left: 0.7em;
    // option::-ms-expand{ display: none; }
}
 .formBtns{
     padding: 0 5px;
     margin-top: 15px;
     button{
         width: 100%;
     }
 }
 .el-form-item{
     margin-bottom: 0;
 }
 .wifiForm div{
    line-height:normal;
 }
 
  .el-form-item__content{
        line-height: 20px;
    }
</style>